package client;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * Created by hasee on 2016/9/23.
 */
public class MinaClient {
    private static final int PORT = 9123;
    public static void main(String[] args) throws Throwable  {
        NioSocketConnector connector = new NioSocketConnector();
        // 设置连接超时检查时间
        connector.setConnectTimeoutCheckInterval(30);

            connector.getFilterChain().addLast("codec",
                    new ProtocolCodecFilter(new TextLineCodecFactory( Charset.forName( "UTF-8" ))));

        connector.getFilterChain().addLast("logger", new LoggingFilter());
        connector.setHandler(new MinaClientHandler());
        IoSession session;
        ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", PORT));
        future.awaitUninterruptibly();
//        future.getSession().write("Hi Server!");
//        future.getSession().write("quit");

        // 等待连接断开
        future.getSession().getCloseFuture().awaitUninterruptibly();
        // 释放连接
        connector.dispose();
    }
}
